var areaTable;

$.fn.dataTableExt.oApi.fnReloadAjax = function(oSettings, sNewSource, fnCallback, bStandingRedraw)
{
    if (typeof sNewSource != 'undefined' && sNewSource != null) {
        oSettings.sAjaxSource = sNewSource;
    }

    // Server-side processing should just call fnDraw
    if (oSettings.oFeatures.bServerSide) {
        this.fnDraw();
        return;
    }

    this.oApi._fnProcessingDisplay(oSettings, true);
    var that = this;
    var iStart = oSettings._iDisplayStart;
    var aData = [];

    this.oApi._fnServerParams(oSettings, aData);

    oSettings.fnServerData.call(oSettings.oInstance, oSettings.sAjaxSource, aData, function(json) {
        /* Clear the old information from the table */
        that.oApi._fnClearTable(oSettings);

        /* Got the data - add it to the table */
        var aData = (oSettings.sAjaxDataProp !== "") ?
                that.oApi._fnGetObjectDataFn(oSettings.sAjaxDataProp)(json) : json;

        for (var i = 0; i < aData.length; i++)
        {
            that.oApi._fnAddData(oSettings, aData[i]);
        }

        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();

        if (typeof bStandingRedraw != 'undefined' && bStandingRedraw === true)
        {
            oSettings._iDisplayStart = iStart;
            that.fnDraw(false);
        }
        else
        {
            that.fnDraw();
        }

        that.oApi._fnProcessingDisplay(oSettings, false);

        /* Callback user function - for event handlers etc */
        if (typeof fnCallback == 'function' && fnCallback != null)
        {
            fnCallback(oSettings);
        }
    }, oSettings);
};

function serverList() {
    $('<a class="_hootified" a="" href="#" #area_table"="" onclick="javascript:var e = document.createEvent("CustomEvent"); e.initCustomEvent("hootletEvent", true, true, {type: "hash", value: "#area_table"});  document.body.dispatchEvent(e); return false;">#area_table</a>').dataTable({
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/admin/areaacademica/list"
    });
}

function list() {
    $.ajax({
        url: '/admin/areaacademica/list',
        type: 'post',
        dataType: 'json',
        success: function(response) {
            // se traen los datos al cargar la pagina
            areaTable = $('#area_table').dataTable({
                "sDom": "<'nuevo'> frtip",
                "sPaginationType": "bootstrap",
                "oLanguage": {
                    "sLengthMenu": "_MENU_ records per page"
                },
                "aaData": response,
                "aoColumns": [
                    {
                        "sTitle": ""
                    },
                    {
                        "sTitle": "Nombre"
                    },
                    {
                        "sTitle": "Descripcion"
                    }
                ]
            });
            $("div.nuevo").html('<button id="new" class="btn btn-large btn-primary" onclick="nuevo();" type="button" name="new">Nuevo</button>');
            $.unblockUI();
        },
        error: function(event, request, settings) {
            $.unblockUI();
            console.log(event);
            console.log(request);
            console.log(settings);
        }
    });

}
$(document).ready(function() {

    $.blockUI({
        message: "Aguarde un momento por favor"
    });
    list();
});
